package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingApi;
import com.tanhua.model.domain.Question;
import com.tanhua.model.domain.Settings;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SettingsService {

    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private SettingApi settingApi;

    @DubboReference
    private BlackListApi blackListApi;


    /*用户通用设置---读取*/
    public SettingsVo settings() {
        SettingsVo settingsVo = new SettingsVo();
        //获取用户ID和手机号
        Long userId = UserHolder.getUserId();
        String mobile = UserHolder.getMobile();
        settingsVo.setId(userId);
        settingsVo.setPhone(mobile);
        //获取用户的陌生人问题
        Question question = questionApi.findByUserId(userId);
        String txt = question == null ? "你喜欢java吗！" : question.getTxt();
        settingsVo.setStrangerQuestion(txt);
        //获取用户的APP通知开关数据
        Settings settings = settingApi.findByUserId(userId);
        if (settings != null) {
            settingsVo.setGonggaoNotification(settings.getGonggaoNotification());
            settingsVo.setLikeNotification(settings.getLikeNotification());
            settingsVo.setPinglunNotification(settings.getPinglunNotification());
        }
        return settingsVo;
    }

    /*设置陌生人问题---保存*/
    public void saveQuestions(String content) {
        //获取当前用户ID
        Long userId = UserHolder.getUserId();
        //查询当前陌生人问题
        Question question = questionApi.findByUserId(userId);
        //不存在保存(新增)
        if (question == null) {
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            questionApi.save(question);
        }
        //存在则更新
        else {
            question.setTxt(content);
            questionApi.update(question);
        }
    }

    /*通知设置---保存*/
    public void saveSettings(Map map) {
        boolean likeNotification = (boolean) map.get("likeNotification");
        boolean pinglunNotification = (boolean) map.get("pinglunNotification");
        boolean gonggaoNotification = (boolean) map.get("gonggaoNotification");

        //获取当前用户ID
        Long userId = UserHolder.getUserId();
        //查询用户当前通知设置
        Settings settings = settingApi.findByUserId(userId);
        //不存在保存(新增)
        if (settings == null) {
            settings = new Settings();
            settings.setUserId(userId);
            settings.setPinglunNotification(pinglunNotification);
            settings.setLikeNotification(likeNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settingApi.save(settings);
        }
        //存在则更新
        else {
            settings.setPinglunNotification(pinglunNotification);
            settings.setLikeNotification(likeNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settingApi.update(settings);
        }
    }

    /*黑名单---翻页列表*/
    public PageResult blacklist(int page, int size) {
        //获取当前用户ID
        Long userId = UserHolder.getUserId();
        //调用API查询用户的黑名单分页列表
        IPage<UserInfo> iPage = blackListApi.findByUserId(userId, page, size);
        //将查询的Ipage对象的内容封装到PageResult中,并返回
        PageResult pr = new PageResult(page,size,iPage.getTotal(),iPage.getRecords());
        return pr;
    }

    /*黑名单---移除*/
    public void deleteBlackList(Long blackUserId) {
        //获取当前用户ID
        Long userId = UserHolder.getUserId();
        //调用API删除
        blackListApi.delete(userId,blackUserId);
    }
}
